<template>
  <div class="mask">
    <el-card class="artical-add-container">
      <div slot="header" class="header">
        <!-- 职位预置项表单-->
        <el-form
          ref="article"
          :model="article"
          label-width="80px"
          style="width: 20%"
        >
          <el-form-item label="职位标题:">
            <el-input
              v-model.trim="article.title"
              placeholder="请填写职位标题:"
            ></el-input>
          </el-form-item>
          <el-form-item label="职位归类:">
            <el-select
              v-model.trim="article.categoryId"
              placeholder="请选择职位类别"
            >
              <el-option
                v-for="item in categorys"
                :key="item.id"
                :label="item.category_name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="是否推荐:" prop="delivery">
            <el-switch v-model.trim="article.isRecommend"></el-switch>
          </el-form-item>
        </el-form>
        <!-- 职位描述表单 -->
        <el-form
          ref="article"
          :model="article"
          label-width="80px"
          style="width: 40%"
        >
          <el-form-item label="职位描述:">
            <el-input
              type="textarea"
              v-model.trim="article.description"
              placeholder="请填写职位描述"
              style="height: 100px"
            ></el-input>
          </el-form-item>
        </el-form>
        <!-- 职位封面上传 -->
        <el-upload
          class="uploader"
          name="coverPicture"
          action="http://localhost:3000/upload/coverPicture/"
          :show-file-list="true"
          :limit="1"
          :on-exceed="handleImageExeed"
          :on-success="handleImageSuccess"
          :before-upload="beforeImageUpload"
        >
          <img
            v-if="imageUrl"
            :src="imageUrl"
            class="picture"
            style="width: 100%"
          />
          <i v-else class="uploader-tip">点击添加职位封面 </i>
        </el-upload>
      </div>
      <div class="button">
        <el-button type="primary" @click="handleAdd">发布</el-button>
      </div>
      <div class="main">
        <!-- 富文本编辑器-双向数据绑定 -->
        <quill-editor
          :options="editorOption"
          :content="article.content"
          @change="onEditorChange($event)"
          style="width: 100%; height: 300px"
        >
        </quill-editor>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "ArticleAdd",
  data() {
    return {
      article: {
        title: "",
        description: "",
        categoryId: "",
        isRecommend: false,
        coverPicture: "",
        content: "",
      },
      imageUrl: "",
      categorys: [],
      editorOption: {
        // quill模块配置项,可以修改和扩展富文本编辑器的功能
      },
    };
  },

  methods: {
    // 富文本输入改变处理函数
    onEditorChange({ quill, html, text }) {
      this.article.content = html;
    },
    // 封面图片上传处理函数
    handleImageSuccess(res, file) {
      this.article.coverPicture = res.result.path;
      this.imageUrl = res.result.path;
    },
    handleImageExeed(files, fileList) {
      if (files.length === 1) {
        this.$message.error("只能上传一张图片");
      }
    },
    beforeImageUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 5;
      if (["image/jpeg", "image/jpg", "image/png"].indexOf(file.type) === -1) {
        this.$message.error("上传头像图片只能是 JPG , PNG , JPEG 格式!");
        return false;
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 5MB!");
        return false;
      }
    },
    handleAdd() {
      let newArticle = {
        title: this.article.title,
        cover_picture: this.article.coverPicture,
        description: this.article.description,
        content: this.article.content,
        category_id: this.article.categoryId,
        is_recommend: this.article.isRecommend ? 1 : 0,
      };
      //给提示框后确认发布职位
      this.$confirm("此操作将确认发布该篇职位, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http.post("/articles", newArticle).then((res) => {
            if (res.code == 0) {
              this.$message({
                type: "success",
                message: "发布成功!",
              });
              this.$router.push("category");
            } else {
              // console.log(res)
              this.$message({
                type: "error",
                message: "输入内容为空,发布失败!",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消发布",
          });
        });
    },
  },
  created() {
    // 获取全部分类
    this.$http.get("/categorys").then((res) => {
      if (res.code == 0) {
        this.categorys = res.result;
      } else {
        // console.log(res.msg)
      }
    });
  },
};
</script>

<style lang="stylus">
// 引入主题配色
@import 'styles/theme.styl';

.artical-add-container {
  width: 80%;
  margin: 10% auto;
  z-index: 999;
}

.header {
  display: flex;
  justify-content: space-between;

  .el-upload {
    width: 200px;
    height: 180px;
    border: 1px solid #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;

    .uploader-tip {
      font-size: 16px;
      color: #ccc9db;
      width: 178px;
      height: 178px;
      line-height: 178px;
      text-align: center;
    }

    .picture {
      width: 178px;
      height: 178px;
      display: block;
    }
  }
}

.main {
  display: flex;
  justify-content: space-between;

  .quill-editor {
    margin-bottom: 40px;
  }
}

.button {
  display: flex;
  justify-content: left;
  margin-bottom: 20px;
}
</style>
<style>
textarea {
  min-height: 180px;
}
</style>
